package com.framework.main.sys.user.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.framework.main.sys.module.domain.ModuleOperationVO;
import com.framework.main.sys.module.domain.ModuleVO;
import com.framework.main.sys.user.domain.SysDataAuthority;

/**
 * @Description:  用户管理:权限页面显示
 * @Copyright: Copyright (c) 2011

 * @date:Jun 14, 2011
 * @version: 1.0
 */
@SuppressWarnings("unchecked")
public class RecurResource {
	private StringBuffer output = new StringBuffer(1000);
	
	private List allModules = new ArrayList();
	private Map moduleOperations = new HashMap();

	private int position = 0;
	private String printString;

	public List selectedModules;
	public List selectedOperations;
	public List selectedDataAuthority;
	
	public RecurResource() throws Exception {
		allModules = ModuleOperation.allModules();
		moduleOperations = (Map) ModuleOperation.moduleOperations().get(0);
	}

	public void recurResource(String resourceID) {
		for (int i = 0; i < allModules.size(); i++) {
			ModuleVO moduleVO = (ModuleVO) allModules.get(i);
			if (resourceID.equalsIgnoreCase(moduleVO.getParentIdf())) {
				boolean isHasChildren = isHasChildren(moduleVO.getModuleIdf());
				if(position==0||position==1){
					output.append("<tr class=\"td1\">\n<td>\n");
				}else{
					output.append("<tr class=\"td2\" onmouseover=\"changeBgColorOnMouseOver(this);\" onmouseout=\"changeBgColorOnMouseOut(this);\">\n<td>\n");
				}
				output.append(echoSpace(position * 2));

				// ModuMana End
				output.append("<input type=\"checkbox\" name =\"r" + moduleVO.getParentIdf() + "\"");
				if (isHasResourceRight(moduleVO.getModuleIdf())) {
					output.append(" checked");
				}
				

				output.append(" id=\"r" + moduleVO.getParentIdf() + "\" cid=\"r"
						+ moduleVO.getModuleIdf()
						+ "\" onclick=\"check(this);\"" + "value=\""
						+ moduleVO.getModuleIdf() + ","+moduleVO.getModuleIdf()+"," + moduleVO.getNamef() + "\">"
						+ "<b>" + moduleVO.getNamef() + "</b>");
				if(position==0||position==1){
					if(isHasModule(moduleVO.getModuleIdf())){
					output
					.append("</td><td><b>数据权限：</b>&nbsp;&nbsp;<select style=\" width:100px;\" id=\"a"+moduleVO.getModuleIdf()+"\" onchange=\"jointAll(this)\"><option value=\"0\">全部</option><option value=\"1\">部门</option><option value=\"2\">个人</option></select></td>");
					}else{
						output
						.append("</td><td>&nbsp;</td>");
					}
				}
				// ModuMana End

				// Operation Begin
				List oprList = (ArrayList) moduleOperations.get((moduleVO
						.getModuleIdf()));
				if (oprList.size() > 0) {
					output.append("&nbsp;");
					output.append(echoLine(5));
					output.append("&nbsp;");

					for (int j = 0; j < oprList.size(); j++) {
						ModuleOperationVO moduleOperationVO = (ModuleOperationVO) oprList
								.get(j);
						output.append("<input name=\"moduAndOper\"");
						if (isHasOperationRight(moduleOperationVO.getOperationIdf())) {
							output.append(" checked");
						}
						String cid, id;
						cid = "o" + moduleVO.getModuleIdf();
						id = "r" + moduleVO.getModuleIdf();

						output.append(" value=\"" + moduleVO.getModuleIdf()
								+","+moduleOperationVO.getOperationIdf() 
								+ ","+ moduleVO.getNamef() +"-"+ moduleOperationVO.getNamef()
								+ "\" type=\"checkbox\" cid=\"" + cid
								+ "\" id=\"" + id + "\""
								+ "   onclick=\"javascript:checkUp(this);\">");
						output.append("" + moduleOperationVO.getNamef() + "");
						output.append("&nbsp;");
					}
//					if(isHasModule(moduleVO.getParentIdf())){
//						output
//						.append(echoSpace(2 * 2))
//						.append("<select id=\"a"+moduleVO.getModuleIdf()+"\" onchange=\"joint(this)\"><option value=\"0\">全部</option><option value=\"1\">部门</option><option value=\"2\">个人</option></select>");
//					}
					
				}
				if(!moduleVO.getParentIdf().equals("-1")&&!moduleVO.getParentIdf().equals("-2")&&isHasModule(moduleVO.getParentIdf())){
					output
					.append(echoSpace(2 * 2))
					.append("</td><td>"+echoSpace(12)+"<select style=\" width:100px;\" id=\"a"+moduleVO.getModuleIdf()+"\" onchange=\"joint(this)\"><option value=\"0\">全部</option><option value=\"1\">部门</option><option value=\"2\">个人</option></select></td>");
				}else{
					if(!(moduleVO.getParentIdf().equals("-1")||moduleVO.getParentIdf().equals("-2"))){
						output
						.append("</td><td>&nbsp;</td>");
					}
				}
				for(int k = 0;k<selectedDataAuthority.size();k++){
					SysDataAuthority sda = (SysDataAuthority) selectedDataAuthority.get(k);
					if(sda.getMenuIdf().equals(moduleVO.getModuleIdf())){
						output.append("<script>document.getElementById(\"a"+moduleVO.getModuleIdf()+"\").options["+sda.getDataScopef()+"].selected = true;</script>");
						break;
					}
				}
				// Operation End

				if (isHasChildren) {
					position++;
					recurResource(moduleVO.getModuleIdf());

					position--;
				}
				output.append("</td>\n</tr>\n");
			}
		}

	}

	public String echoSpace(int count) {
		StringBuffer output = new StringBuffer(20);
		for (int i = 0; i < count; i++) {
			output.append("&nbsp;&nbsp;");
		}
		return output.toString();
	}

	public String echoLine(int count) {
		StringBuffer output = new StringBuffer(20);
		for (int i = 0; i < count; i++) {
			output.append("<span style=\"font-size:6px\">.....</span>");
		}
		return output.toString();
	}

	public boolean isHasChildren(String moduleIdf) {
		for (int i = 0; i < allModules.size(); i++) {
			ModuleVO moduleVO = (ModuleVO) allModules.get(i);
			if (moduleIdf.equalsIgnoreCase(moduleVO.getParentIdf())) {
				return true;
			}
		}
		return false;
	}

	public boolean isHasResourceRight(String resourceID) {
		for (int i = 0; i < selectedModules.size(); i++) {
			String re = selectedModules.get(i).toString();
			if (resourceID.equalsIgnoreCase(re))
				return true;
		}
		return false;
	}

	public boolean isHasOperationRight(String operationID) {
		for (int i = 0; i < selectedOperations.size(); i++) {
			String permission = selectedOperations.get(i).toString();
			if (operationID.equalsIgnoreCase(permission.toString()))
				return true;
		}
		
		return false;
	}
	public boolean isHasModule(String ModuleIdf){
		if(ModuleIdf.equals("code")||ModuleIdf.equals("meetingtaskmanagement")||ModuleIdf.equals("sysmanagement")){
			return false;
		}
		return true;
	}
	public void setPrintString(String printString) {
		this.printString = printString;
	}

	public String getPrintString() {
		if (printString == null) {
			printString = this.output.toString();
		}
		return printString;
	}

}
